import React, { useEffect } from "react";
import { modalPropsType } from "@/constants/types";
import { Modal, Form, Input } from "antd";
import { isEmptyObj } from "@/utils/util";

const {Item} = Form;
const rules = [{required: true, message: '此项必填'}]

const MenuUpdateModal = ({visible, onCancel, onSubmit, parentRow, loading, title}: modalPropsType) => {
  const [form] = Form.useForm();

  const handleSubmit = async () => {
    const res = await form.validateFields();
    onSubmit({
      ...res,
      parentId: parentRow?.id
    });
  }

  useEffect(() => {
    if (!isEmptyObj(parentRow)) {
      form.setFieldsValue({
        parentDeptName: parentRow?.name
      });
    }
  }, [JSON.stringify(parentRow?.name)])

  return (
    <Modal
      title={title}
      open={visible}
      onCancel={onCancel}
      onOk={handleSubmit}
      confirmLoading={loading}
    >
      <Form form={form}>
        <Item name="parentDeptName" label="上级部门" rules={rules}>
          <Input disabled />
        </Item>
        <Item name="name" label="部门名称" rules={rules}>
          <Input />
        </Item>
      </Form>
    </Modal>
  );
}
export default MenuUpdateModal;